﻿using BLL;
using CrystalDecisions.CrystalReports.Engine;
using gfLibrary;
using IYAKASAPortal.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal.PrintAll
{
    public partial class PrintZBSJ : System.Web.UI.Page
    {
        private UserInfo userInfo { get; set; }
        private ReportDocument document;


        protected void Page_Load(object sender, EventArgs e)
        {

            string strLineNum = string.Empty, strDevCalibName = string.Empty, strBDRQ = string.Empty, strDevName = string.Empty;
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            {
                string userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }
            if (Request["JCXH"] != null && !string.IsNullOrEmpty(Request["JCXH"]) && Request["DevCalibName"] != null && !string.IsNullOrEmpty(Request["DevCalibName"]))
            {
                strLineNum = Request["JCXH"].ToString();
                strDevCalibName = Request["DevCalibName"].ToString();
                strBDRQ = Request["BDRQ"].ToString();
                strDevName = Request["DevName"].ToString();              
            }
            BindReportData(strLineNum, strDevCalibName, strBDRQ, strDevName);
         
        }

        private void BindReportData(string strLineNum, string strDevCalibName, string strBDRQ, string strDevName)
        {

            string strPath = Server.MapPath("~/rpt/CrystalReportZBSJ2.rpt");
            document = BindZBSJ_Report(strPath, strLineNum, strDevCalibName, strBDRQ, strDevName);
            ZBSJ.ReportSource = document;
            ZBSJ.DataBind();
            ZBSJ.RefreshReport();
        }


        public ReportDocument BindZBSJ_Report(string strPath, string strLineNum, string strDevCalibName, string strBDRQ, string strDevName)
        {

            CALIB_DATA_BLL bll = new CALIB_DATA_BLL();
            ReportDocument document = new ReportDocument();
            document.Load(strPath);   
            rpt.DsZBSJ ds = new rpt.DsZBSJ();

            string strSql = string.Format("JCXH= '{0}' and DevCalibName= '{1}' and convert(varchar(10),StartTime,120) = '{2}' and DevName = '{3}'", strLineNum, strDevCalibName, strBDRQ, strDevName);

            var ZBSJ = bll.GetModelList(strSql);          
           

            
            string strName = string.Empty;
            string strJCXH = string.Empty;
            string strZBRQ = string.Empty;
            for (int i = 0; i < ZBSJ.Length; i++)
            {
                DataRow dr = ds.Tables["DtZBSJBody"].NewRow();
                dr["DYZ"] = ZBSJ[i].DYZ;
                dr["CLZ"] = ZBSJ[i].CLZ;
                dr["BZZ"] = ZBSJ[i].BZZ;
                dr["JDWC"] = ZBSJ[i].JDWC;
                dr["XDWC"] = ZBSJ[i].XDWC;
                ds.Tables["DtZBSJBody"].Rows.Add(dr);
             
            }

            DataRow drHead = ds.Tables["DtZBSJHead"].NewRow();
            drHead["NAME"] = string.Format("[{0}]{1}", strDevName, strDevCalibName);
            drHead["JCXH"] = strLineNum;
            drHead["ZBRQ"] = strBDRQ;

            ds.Tables["DtZBSJHead"].Rows.Add(drHead);

             document.SetDataSource(ds);
            return document;  
        }
        protected override void OnUnload(EventArgs e)
        {
            document.Dispose();
            base.OnUnload(e);
        }
    }
}